﻿using System.ServiceModel;
using System.Runtime.Serialization;
using System.Transactions;

#region Interface do Serviço
[ServiceContract(SessionMode = SessionMode.Required)] // também com Allowed temos sessionless
public interface IServicoMSMQ {

    [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
    void inserirConta(Conta c);

    [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
    void alterarConta(Conta c);

    [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = true)]
    void terminaEnvio();
}
#endregion

#region DTO
[DataContract(Namespace = "MeuNamespace")]
public class Conta {
    [DataMember(IsRequired = true)]
    public int Numero { get; set; }
    [DataMember(IsRequired = true)]
    public string Titular { get; set; }
    [DataMember(IsRequired = true)]
    public float Saldo { get; set; }
}
#endregion

namespace ClienteTrComMSMQ {
    class Program {
        static void Main(string[] args) {

            ChannelFactory<IServicoMSMQ> servico = new ChannelFactory<IServicoMSMQ>("clienteServicoMSMQ");
            IServicoMSMQ endpt = servico.CreateChannel();

            Conta c = new Conta { Numero = 1, Titular = "msmq1", Saldo = 0 };
            Conta c2 = new Conta { Numero = 1111, Titular = "msmq2", Saldo = 0 };

            using (TransactionScope ts = new TransactionScope()) {
                endpt.alterarConta(c);
                endpt.alterarConta(c2);
                endpt.terminaEnvio();
                ts.Complete();
            }
        }
    }
}
